package timing.ukulele.third.msgChannel.action.send;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import timing.ukulele.third.dictionary.msg.MsgChannelType;
import timing.ukulele.third.msgChannel.handle.HandlerHolder;
import timing.ukulele.third.msgChannel.model.SendTaskInfo;
import timing.ukulele.third.msgChannel.param.HandleMessageBaseResponse;

/**
 * 发送消息，路由到对应的渠道下发消息
 *
 * @author zh
 */
@Service
public class SendMessageDirectAction {

    @Autowired
    private HandlerHolder handlerHolder;

    public void process(SendTaskInfo sendTaskInfo) {

        Integer sendChannel = sendTaskInfo.getSendChannel();

        // 微信小程序&服务号只支持单人推送，为了后续逻辑统一处理，于是在这做了单发处理
        if (MsgChannelType.MINI_PROGRAM.getCode().equals(sendChannel)
                || MsgChannelType.OFFICIAL_ACCOUNT.getCode().equals(sendChannel)
                || MsgChannelType.ALIPAY_MINI_PROGRAM.getCode().equals(sendChannel)) {

            SendTaskInfo taskClone = ObjectUtil.cloneByStream(sendTaskInfo);
            for (String receiver : sendTaskInfo.getReceiver()) {
                taskClone.setReceiver(Sets.newHashSet(receiver));
                handlerHolder.route(sendChannel).send(taskClone);
            }
            return;
        }
        handlerHolder.route(sendChannel).send(sendTaskInfo);
    }

}
